<%-- 
    Document   : cuenta
    Created on : 11-jun-2009, 13:46:39
    Author     : Ale
--%>

<%@page contentType="text/html" pageEncoding="UTF-8" session="false" import="domain.*, planner.*, java.util.Iterator, java.util.Set, java.util.List, java.sql.Date"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>DOMECO</title>
        <link href="style.css" rel="stylesheet" type="text/css">


    </head>
    <body>

        <%if (request.getSession(false) != null) {%>
        <table align="center" width="1000" >
            <tr>
                <td>
                    <div id="menu" align="center"><jsp:include page="./menu principal.jsp"></jsp:include></div>
                    <br>

                    <p id="campoAzul" align="center" valign="middle" width="235px"><span id="blanco">Estado actual de la Cuenta</span><br></p>
                    <div id="general">
                        <div id="cuenta" align="center" >
                            <%
    Userdom user = (Userdom) request.getSession().getAttribute("user");
    Operations o = new Operations();
    Set cuentas = o.getBankaccountsList(user);
    double saldoTotal = 0;
    String name = "";
    boolean ocultar = false;
    boolean ocultar2 = true;
    boolean facturas = false;
    double totalI = 0;
    double totalE = 0;
    double totalFacturas = 0;
    if (cuentas.isEmpty()) {
        ocultar = true;
                            %>
                            <br>
                            <br>
                            <p align=right"><span id="rojo"> Noy hay Cuentas</span></p>
                            <%} else {
                                Iterator it1 = cuentas.iterator();
                                while (it1.hasNext()) {
                                    Bankaccount b = (Bankaccount) it1.next();
                                    name = b.getName();

                            %>
                            <form action="./cuenta.jsp">
                                <p align=center"> <span id="datosCuenta"> Nombre de la Cuenta: <%=name%> </span> </p>
                                <br>
                                <table id="tabla" align="center" width="120px">
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Concepto</span></td>
                                        <td align="center"><span id="blanco" >Mes: </span>
                                            <select name="month" id="listaDesplegable">
                                                <option value="0" id="listaDesplegable">Enero</option>
                                                <option value="1" id="listaDesplegable">Febrero</option>
                                                <option value="2" id="listaDesplegable">Marzo</option>
                                                <option value="3" id="listaDesplegable">Abril</option>
                                                <option value="4" id="listaDesplegable">Mayo</option>
                                                <option value="5" id="listaDesplegable">Junio</option>
                                                <option value="6" id="listaDesplegable">Julio</option>
                                                <option value="7" id="listaDesplegable">Agosto</option>
                                                <option value="8" id="listaDesplegable">Septiembre</option>
                                                <option value="9" id="listaDesplegable">Octubre</option>
                                                <option value="10" id="listaDesplegable">Noviembre</option>
                                                <option value="11" id="listaDesplegable">Diciembre</option>
                                        </select> </td>
                                        <td align="center"><span id="blanco">en Euros</span></td>
                                    </tr>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Ingresos</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                    </tr>
                                    <%
                                Set incomes = b.getIncomes();
                                boolean bool = false;

                                //Vamos a iterar sobre los ingresos
                                if (!incomes.isEmpty()) {
                                    Iterator it2 = incomes.iterator();
                                    totalI = 0;
                                    while (it2.hasNext()) {
                                        Income i = (Income) it2.next();
                                        if (i.getDateIncome().getMonth() == Integer.parseInt(request.getParameter("month")) && i.getDateIncome().getYear() == new java.util.Date().getYear()) {
                                            bool = true;
                                            double amount = i.getAmount();
                                            saldoTotal += amount;
                                            totalI += amount;
                                            String concept = i.getConcept();
                                            java.sql.Date date = i.getDateIncome();
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span onclick="abrirVentanaIngreso()" ><%=concept%></span></td>
                                        <td><span><%=date%></span></td>
                                        <td><span><%=amount%>€<a href="Delete?type=income&idIncome=<%out.print(i.getIdIncome());%>&redir=C">   <img src="./Imagenes/borrar.jpg"></a></span></td>
                                    </tr>
                                    <%
                                            }
                                        }
                                        //Ahora especificamos el total de ingresos
                                        if (bool) {
                                            List contratos = o.getWContrList(user);
        if(!contratos.isEmpty()){
            Iterator it4 = contratos.iterator();
            while(it4.hasNext()){
                Wcontract cont = (Wcontract) it4.next();
                Salary sal = new Operations().getSalaryDate(cont);
                ocultar2 = false;
                if(sal == null){
                    totalI += cont.getBasicSalary();%> <span id="rojo">Incluido el Salario Básico de su contrato de trabajo. Revise el Salario de este mes.</span><br><br> <%
                }else if (sal.getEnumeration() == 2){
                    totalI += sal.getAmount();
                    %>  <span id="rojo">Incluido el Salario revisado.</span><br><br>  <%
                }else if (sal.getEnumeration() == 1){
                    %>  <span id="rojo">Salario revisado, pero no cobrado. Sueldo no incluido en la cuenta.</span><br><br>  <%
}}}
                                    %>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco">Total de Ingresos: +<% out.println(totalI);%> €</span></td>
                                    </tr>
                                    <%} else {%>
                                    <tr id="datosCuenta"><td></td><td> <span id="rojo"> No hay Ingresos en <%=b.getName()%></span> </td><td></td></tr>

                                    <%  }


                                    } else {
                                    %>
                                    <tr id="datosCuenta"><td></td><td> <span id="rojo"> No hay Ingresos en <%=b.getName()%></span> </td><td></td></tr>
                                    <%                                }%>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Gastos</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                    </tr>
                                    <%Set expenses = b.getExpenses();
                                bool = false;
                                //Ahora vamos a iterar sobre los gastos
                                if (!expenses.isEmpty()) {
                                    Iterator it2 = expenses.iterator();
                                    totalE = 0;
                                    while (it2.hasNext()) {
                                        Expense e = (Expense) it2.next();
                                        if (e.getDateExpense().getMonth() == Integer.parseInt(request.getParameter("month")) && e.getDateExpense().getYear() == new java.util.Date().getYear()) {
                                            bool = true;
                                            double amount = e.getAmount();
                                            saldoTotal -= amount;
                                            totalE += amount;
                                            String concept = e.getConcept();
                                            java.sql.Date date = e.getDateExpense();
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span onclick="abrirVentanaIngreso()" ><%=concept%></span></td>
                                        <td><span><%=date%></span></td>
                                        <td><span><%=amount%>€<a href="Delete?type=expense&idExpense=<%out.print(e.getIdExpense());%>&redir=C">            <img src="./Imagenes/borrar.jpg"></a></span></td>
                                    </tr>
                                    <%
                                            }
                                        }
                                        //Ahora especificamos el total de los gastos
                                        if (bool) {
                                    %>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco">Total de Gastos: <% out.println(totalE);%> €</span></td>
                                    </tr>
                                    <%} else {%>
                                    <tr id="datosCuenta"><td></td><td><span id="rojo"> No hay Gastos en <%=b.getName()%></span> </td><td></td></tr>

                                    <%  }


                                    } else {
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <tr id="datosCuenta"><td></td><td> <span id="rojo"> No hay Gastos en <%=b.getName()%></span> </td><td></td></tr>
                                    </tr>
                                    <%                                }%>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Facturas</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                    </tr>

                                    <%//Recorremos las facturas de Electricidad
                                List l = o.getElecContrList(user);
                                Iterator itElec = l.iterator();
                                totalFacturas = 0;
                                while (itElec.hasNext()) {
                                    Electricitycontract e = (Electricitycontract) itElec.next();
                                    double power = e.getContratedPower();

                                    Electricityconsumption eCons = new Operations().getLastElecConsumption(e);
                                    if (eCons != null) {
                                        totalFacturas += eCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span>(Electricidad) <%=e.getCompany()%></span></td>
                                        <td><span><%=eCons.getDateFrom()%> --- <%=eCons.getDateTo()%></span></td>
                                        <td><span><%=eCons.getInvoice().getAmount()%> €</span></td>
                                    </tr>

                                    <%}
                                }
                                //Recorremos las facturas de Hipotecas
                                List lh = o.getMortgageContrList(user);
                                Iterator it1h = lh.iterator();
                                while (it1h.hasNext()) {
                                    Mortgagecontract e = (Mortgagecontract) it1h.next();

                                    Mortgageconsumption mCons = new Operations().getLastMortgConsumption(e);
                                    if (mCons != null) {
                                        totalFacturas += mCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span>(Hipoteca)<%=e.getCompany()%></span></td>
                                        <td><span><%=mCons.getDateFrom()%> --- <%=mCons.getDateTo()%></span></td>
                                        <td><span><%=mCons.getInvoice().getAmount()%> €</span></td>
                                    </tr>

                                    <%
                                    }
                                }
                                //Recorremos las facturas de Seguros
                                List ls = o.getInsuranceContrList(user);
                                Iterator it1s = ls.iterator();
                                while (it1s.hasNext()) {
                                    Insurancecontract e = (Insurancecontract) it1s.next();
                                    Insuranceconsumption iCons = new Operations().getLastInsConsumption(e);
                                    if(iCons != null){
                                        totalFacturas += iCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span>(Seguro)<%=e.getCompany()%></span></td>
                                        <td><span><%=iCons.getDateFrom() %> --- <%=iCons.getDateTo() %></span></td>
                                        <td><span><%=iCons.getInvoice().getAmount() %> €</span></td>
                                    </tr>

                                    <%
                                                    }
                                                }
                                //Recorremos las facturas de agua
                                List lw = o.getWaterContrList(user);
                                Iterator it1w = lw.iterator();
                                while (it1w.hasNext()) {
                                    Watercontract e = (Watercontract) it1w.next();
                                    Waterconsumption wCons = new Operations().getLastWaterConsumption(e);
                                    if(wCons != null){
                                        totalFacturas += wCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span><%=e.getCompany() %></span></td>
                                        <td><span><%=wCons.getDateFrom()%> --- <%=wCons.getDateTo()%></span></td>
                                        <td><span><%=wCons.getInvoice().getAmount() %> €</span></td>
                                    </tr>

                                    <%
                                                    }
                                                }


                                //Recorremos las facturas de movil
                                List lfact = o.getMobileContrList(user);
                                Iterator itF = lfact.iterator();
                                while (itF.hasNext()){
                                    Mobilecontract mc = (Mobilecontract) itF.next();

                                    Mobileconsumption mCons = o.getMobileconsumptionDate(mc);


                                    if(mCons != null){
                                        totalFacturas += mCons.getConsumption().getAmount();
                                        facturas = true;
                                    %>
                                    <tr id="datosCuenta" align="center">
                                        <td><span><%=mc.getCompany() %></span></td>
                                        <td><span><%=mCons.getDateFrom()%> --- <%=mCons.getDateTo()%></span></td>
                                        <td><span><%=mCons.getInvoice().getAmount() %> €</span></td>
                                    </tr>

                                    <%
                                                    }
                                                }



                                if (facturas) {
                                    %>
                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco"></span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco">Total de Facturas: <% out.println(totalFacturas);%> €</span></td>
                                    </tr>
                                    <%

                                }
                                //Calcular el saldo total:
                                saldoTotal = saldoTotal - totalFacturas;


                                    %>
                                </table>
                                <table id="" align="center">
                                    <tr><td><br></td></tr>

                                    <tr id="campoAzul">
                                        <td width="180px" align="center"><span id="blanco">Saldo a su favor</span></td>
                                        <td><span id="blanco"></span></td>
                                        <td><span id="blanco"><% out.println(saldoTotal);%> €</span></td>
                                    </tr>

                                </table>
                                <input type="submit" value="" id="cambiarMes">
                            </form>
                            <%
            //saldoTotal = 0;
        }

    }


    int inc = (int) totalI;
    int exp = (int) totalE;
    int fac = (int) totalFacturas;

                            %>
                            <table id="tabla">
                                <td>
                                    <tr>
                                        <img src="http://chart.apis.google.com/chart?chs=450x150&chd=t:<%
    out.print(inc / 10 + ",");
    out.print(exp / 10 + ",");
    out.print(fac / 10);
                                             %>&cht=p3&chl=<%
    if (inc > 0) {
        out.print("Ingresos: " + inc + "€|");
    }

    if (exp > 0) {
        out.print("Gastos: " + exp + "€|");
    }

    if (fac > 0) {
        out.print("Facturas: " + fac + "€");
    }
                                             %>"/>
                                    </tr>
                                </td>
                            </table>

                        </div>


                        <div id="barraDer">
                            <div id="accesos">
                                <table align="center">
                                    <tr ><td id="campoAzul" align="center" valign="middle" width="235px"><span id="blanco">Operaciones</span><br></td></tr>
                                </table>

                                <p align="center"><a href="./FrontController?redir=NC" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir cuenta</span></a></p>
                                <p align="center">  <a href="./FrontController?redir=NG" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir gasto</span></a></p>
                                <p align="center"> <a href="./FrontController?redir=NI" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>A&ntilde;adir ingreso</span></a> </p>
                                <%if (!ocultar) {%>             <p align="center">  <a href="./FrontController?redir=MBCU" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>Modificar/Borrar Cuentas</span></a></p> <%}%>
                                <%if (ocultar2) {%>             <p align="center">  <a href="./FrontController?redir=MBCL" id="colorEvento"><span onmouseover='this.style.color="#FF9900"' onmouseout='this.style.color="#79A9D9"'>Modificar/Borrar Cont. Laboral</span></a></p> <%}%>
                            </div>

                            <div id="estadisticas">
                                <table align="center">
                                    <tr ><td id="campoAzul" align="center" valign="middle" width="235px"><span id="blanco">Datos actuales</span><br></td></tr>
                                </table>

                                <p align="center"><a href="#" id="colorEvento"><span>Nombre del Banco: <span id="rojo"><%=name%></span></a></p>
                                <p align="center">  <a href="#" id="colorEvento"><span>Saldo a su favor:<span id="rojo"><%=saldoTotal%> €</span></span></a></p>
                                <p align="center"><span id="naranja"><% java.util.Date fecha = new java.util.Date();
    out.println(fecha.toLocaleString());%></span> </p>

                            </div>
                        </div>
                    </div>

                    <div id="abajo" align="center"><p>&copy;   Domeco &mdash; Proyecto Informático A&ntilde;o 2009 </p></div>
                </td>
            </tr>
        </table>
        <%} else {%>
        <jsp:forward page="./index.jsp"></jsp:forward>
        <%}%>
    </body>
</html>
